package it.polimi.traveldream.web.beans.customer;

import it.polimi.traveldream.ejbclient.dtos.TravelPackageDTO;

import it.polimi.traveldream.ejbclient.eaos.TravelPackageServiceEAO;
import it.polimi.traveldream.utils.enums.PackageSearchType;
import it.polimi.traveldream.utils.exceptions.NotSuchProductException;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

@ManagedBean(name="listCustomerTravelPackages")
@SessionScoped
public class ListPackages {
	@EJB
	private TravelPackageServiceEAO tService;
	private List<TravelPackageDTO> travelPackages;
	private List<TravelPackageDTO> travelPackagesSearch;
	private TravelPackageDTO selectedTravelPackage;
	private String keywordSearch;
	private PackageSearchType typeSearch;

	public ListPackages() {
	}
	
	@PostConstruct
	public void init() {
		travelPackages = tService.getAvailablePackages();
	}

	public List<TravelPackageDTO> getTravelPackages() {
		return travelPackages;
	}

	public void setTravelPackages(List<TravelPackageDTO> travelPackages) {
		this.travelPackages = travelPackages;
	}

	public List<TravelPackageDTO> getTravelPackagesSearch() {
		return travelPackagesSearch;
	}

	public void setTravelPackagesSearch(List<TravelPackageDTO> travelPackagesSearch) {
		this.travelPackagesSearch = travelPackagesSearch;
	}

	public TravelPackageDTO getSelectedTravelPackage() {
		return selectedTravelPackage;
	}

	public void setSelectedTravelPackage(TravelPackageDTO selectedTravelPackage) {
		this.selectedTravelPackage = selectedTravelPackage;
	}

	public String getKeywordSearch() {
		return keywordSearch;
	}

	public void setKeywordSearch(String keywordSearch) {
		this.keywordSearch = keywordSearch;
	}

	public PackageSearchType getTypeSearch() {
		return typeSearch;
	}

	public void setTypeSearch(PackageSearchType typeSearch) {
		this.typeSearch = typeSearch;
	}
	
	public PackageSearchType[] getSearchTypes() {
		return PackageSearchType.values();
	}

	public void search() {
		travelPackagesSearch = new ArrayList<TravelPackageDTO>();
		try {
			travelPackagesSearch = tService.searchPackage(typeSearch, keywordSearch);
		} catch (NotSuchProductException e) {
			travelPackages = new ArrayList<TravelPackageDTO>();
		}
	}
	
	public void onTravelPackageRowSelectNavigate(SelectEvent event) {
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedTravelPackage", event.getObject());  
		ConfigurableNavigationHandler nh = (ConfigurableNavigationHandler)FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
		nh.performNavigation("detailTravelPackage?faces-redirect=true"); 
    }
	
}
